package com.zhan.ielts.web.controller;

import com.zhan.frame.core.web.domain.Result;
import com.zhan.frame.core.web.domain.Result.Status;
import com.zhan.ielts.domain.Feedback;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.service.FeedbackService;

import com.zhan.ielts.service.UsersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/feedback")
public class FeedbackController {

	private Logger log = LoggerFactory.getLogger(FeedbackController.class);

	@Autowired
	private FeedbackService feedbackService;

	@Autowired
	private UsersService usersService;

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result fetchTreasureChest(Feedback feedback) {
		if(null == feedback.getUserId() || "".equals(feedback.getUserId())){
			return new Result(Status.ERROR, "反馈信息有误！");
		}
		Users query = new Users();
		query.setId(feedback.getUserId());
		Users users  = usersService.queryOne(query);
		if(users == null){
			return new Result(Status.ERROR, "反馈信息有误！");
		}
		if(null == feedback.getContent() || "".equals(feedback.getContent())){
			return new Result(Status.ERROR, "反馈信息有误！");
		}
		try {
			feedbackService.insert(feedback);
		}catch (Exception e){
			System.out.println(e.getMessage());
			return new Result(Status.ERROR, "反馈失败！");
		}
		return new Result(Status.OK, null);
	}


}
